﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Wedo.Recognition.Domain.Models;
using Wedo.Recognition.Domain.Mapping;
using Wedo.Recognition.Domain;
using Wedo.Domain.Models;
using Wedo.Recognition.Domain.ViewModels;
using EFProviderWrapperToolkit;
using EFCachingProvider.Caching;
using EFCachingProvider;
using System.Data.Common;
using System.Configuration;


namespace Wedo.Recognition.Domain
{
    public partial class EFDbContext:DbContext
    {

        #region 基础数据

        /// <summary>
        /// 银行
        /// </summary>
        public DbSet<Bank> Bank { get; set; }

        public DbSet<SysParam> SysParams { get; set; }

        public DbSet<UserInfo> UserInfo { get; set; }

        public DbSet<Module> Module { get; set; }

        public DbSet<Role> Role { get; set; }

        public DbSet<Resource> Resource { get; set; }

        //public DbSet<RoleUserInfoes> UserRoles { get; set; }

        #endregion

        /// <summary>
        /// 导入时的缓存
        /// </summary>
        public DbSet<IPBankData> IPBankData { get; set; }


        /// <summary>
        /// 银行账单
        /// </summary>
        public DbSet<BankData> BankData { get; set; }

        /// <summary>
        /// 申请争议
        /// </summary>
        public DbSet<DisputeApply> DisputeApply { get; set; }

        
        /// <summary>
        /// 申请认款信息
        /// </summary>
        public DbSet<PaymentApply> PaymentApply { get; set; }

        /// <summary>
        /// 认款附件信息
        /// </summary>
        public DbSet<PaymentAttach> PaymentAttach { get; set; }

        /// <summary>
        /// 放单信息
        /// </summary>
        public DbSet<ReleaseOrder> ReleaseOrder { get; set; }


        /// <summary>
        /// 退款信息
        /// </summary>
        public DbSet<RefundmentApply> RefundmentApply { get; set; }


        /// <summary>
        /// 收款凭证
        /// </summary>
        public DbSet<GenerateRecieve> GenerateRecieve { get; set; }

        public DbSet<CheckRecord> CheckRecord { get; set; }

        /// <summary>
        /// 转账记录
        /// </summary>
        public DbSet<TransferVoucher> TransferVoucher { get; set; }

        /// <summary>
        /// 收款记录
        /// </summary>
        public DbSet<RecieveRecord> RecieveRecord { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add<Bank>(new Bank_Mapping());
            modelBuilder.Configurations.Add<BankData>(new BankData_Mapping());
            modelBuilder.Configurations.Add<DisputeApply>(new DisputeApply_Mapping());

            modelBuilder.Configurations.Add<SysParam>(new SysParam_Mapping());
            modelBuilder.Configurations.Add<PaymentApply>(new PaymentApply_Mapping());
            modelBuilder.Configurations.Add<RefundmentApply>(new RefundmentApply_Mapping());
            modelBuilder.Configurations.Add<ReleaseOrder>(new ReleaseOrder_Mapping());

            modelBuilder.Configurations.Add<PaymentAttach>(new PaymentAttach_Mapping());
            modelBuilder.Configurations.Add<TransferVoucher>(new TransferVoucher_Mapping());
            modelBuilder.Configurations.Add<UserInfo>(new UserInfo_Mapping());
            modelBuilder.Configurations.Add<CheckRecord>(new CheckRecord_Mapping());

            modelBuilder.Configurations.Add<IPBankData>(new IPBankData_Mapping());
            modelBuilder.Configurations.Add<RecieveRecord>(new RecieveRecord_Mapping());
        }

    }

    public partial class EFDbContext
    {
        public EFDbContext()
            : this("name=EFDbContext")
        {

        }
        public EFDbContext(string nameOrConnectionString ) :
            this(ConnectionWrapperUtils.CreateConnectionWrapper(nameOrConnectionString
            , "EFCachingProvider"
            ),true) { }

        public EFDbContext(DbConnection connection, bool isOwener)
            : base(connection,isOwener)
        {

        }
  
        #region Caching Extensions

        private EFCachingConnection CachingConnection
        {
            get { return this.UnwrapConnection<EFCachingConnection>(); }
        }

        public ICache Cache
        {
            get { return CachingConnection.Cache; }
            set { CachingConnection.Cache = value; }
        }

        public CachingPolicy CachingPolicy
        {
            get { return CachingConnection.CachingPolicy; }
            set { CachingConnection.CachingPolicy = value; }
        }

        #endregion

        public override int SaveChanges()
        {
            int result = 0;
            try
            {
                result = base.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {
                List<string> errorList = new List<string>();
                foreach (var item in e.EntityValidationErrors)
                {
                    if (item.IsValid == false)
                    {
                        foreach (var m in item.ValidationErrors)
                        {
                            errorList.Add(m.ErrorMessage);
                        }
                    }
                }
                throw new Exception("验证失败：" + string.Join(".", errorList));
            }
            return result;
        }

    }
}
